home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Shares / SMB.pm < prev   
Encoding:
Perl POD Document  |  2009-04-09  |  8.8 KB  |  356 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. #
  3. # Copyright (C) 2000-2001 Ximian, Inc.
  4. #
  5. # Authors: Hans Petter Jansson <hpj@ximian.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Library General Public License as published
  9. # by the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20.  
  21. package Shares::SMB;
  22.  
  23. use Utils::File;
  24.  
  25. # --- share_export_smb_info; information on a particular SMB export --- #
  26.  
  27. sub gst_share_smb_info_set
  28. {
  29.   my ($info, $key, $value) = @_;
  30.   
  31.   if ($value eq "")
  32.   {
  33.     delete $info->{$key};
  34.   }
  35.   else
  36.   {
  37.     $info->{$key} = $value;
  38.   }
  39. }
  40.  
  41. sub gst_share_smb_info_get_name
  42. {
  43.   return $_[0]->{'name'};
  44. }
  45.  
  46. sub gst_share_smb_info_set_name
  47. {
  48.   &gst_share_smb_info_set ($_[0], 'name', $_[1]);
  49. }
  50.  
  51. sub gst_share_smb_info_get_point
  52. {
  53.   return $_[0]->{'point'};
  54. }
  55.  
  56. sub gst_share_smb_info_set_point
  57. {
  58.   &gst_share_smb_info_set ($_[0], 'point', $_[1]);
  59. }
  60.  
  61. sub gst_share_smb_info_get_comment
  62. {
  63.   return $_[0]->{'comment'};
  64. }
  65.  
  66. sub gst_share_smb_info_set_comment
  67. {
  68.   &gst_share_smb_info_set ($_[0], 'comment', $_[1]);
  69. }
  70.  
  71. sub gst_share_smb_info_get_enabled
  72. {
  73.   return $_[0]->{'enabled'};
  74. }
  75.  
  76. sub gst_share_smb_info_set_enabled
  77. {
  78.   &gst_share_smb_info_set ($_[0], 'enabled', $_[1]);
  79. }
  80.  
  81. sub gst_share_smb_info_get_browse
  82. {
  83.   return $_[0]->{'browse'};
  84. }
  85.  
  86. sub gst_share_smb_info_set_browse
  87. {
  88.   &gst_share_smb_info_set ($_[0], 'browse', $_[1]);
  89. }
  90.  
  91. sub gst_share_smb_info_get_public
  92. {
  93.   return $_[0]->{'public'};
  94. }
  95.  
  96. sub gst_share_smb_info_set_public
  97. {
  98.   &gst_share_smb_info_set ($_[0], 'public', $_[1]);
  99. }
  100.  
  101. sub gst_share_smb_info_get_write
  102. {
  103.   return $_[0]->{'write'};
  104. }
  105.  
  106. sub gst_share_smb_info_set_write
  107. {
  108.   &gst_share_smb_info_set ($_[0], 'write', $_[1]);
  109. }
  110.  
  111.  
  112. # --- share_smb_table; multiple instances of share_smb_info --- #
  113.  
  114. sub smb_table_find
  115. {
  116.   my ($name, $shares) = @_;
  117.  
  118.   foreach $i (@$shares)
  119.   {
  120.     return $i if ($$i[0] eq $name)
  121.   }
  122.  
  123.   return undef;
  124. }
  125.  
  126. sub get_distro_smb_file
  127. {
  128.   my ($smb_comb);
  129.  
  130.   my %dist_map =
  131.   (
  132.    "redhat-6.2"      => "redhat-6.2",
  133.    "redhat-7.0"      => "debian-3.0",
  134.    "redhat-7.1"      => "debian-3.0",
  135.    "redhat-7.2"      => "debian-3.0",
  136.    "redhat-7.3"      => "debian-3.0",
  137.    "redhat-8.0"      => "debian-3.0",
  138.    "mandrake-9.0"    => "debian-3.0",
  139.    "debian-3.0"      => "debian-3.0",
  140.    "debian-3.1"      => "debian-3.0",
  141.    "debian-4.0"      => "debian-3.0",
  142.    "debian-5.0"      => "debian-3.0",
  143.    "debian-testing"  => "debian-3.0",
  144.    "ubuntu-7.04"     => "debian-3.0",
  145.    "suse-9.0"        => "debian-3.0",
  146.    "slackware-9.1.0" => "debian-3.0",
  147.    "gentoo"          => "debian-3.0",
  148.    "archlinux"       => "debian-3.0",
  149.    "pld-1.0"         => "pld-1.0",
  150.    "vine-3.0"        => "debian-3.0",
  151.    "freebsd-5"       => "freebsd-5",
  152.   );
  153.  
  154.   my %dist_tables =
  155.   (
  156.    "redhat-6.2" => "/etc/smb.conf",
  157.    "debian-3.0" => "/etc/samba/smb.conf",
  158.    "pld-1.0"    => "/etc/smb/smb.conf",
  159.    "freebsd-5"  => "/usr/local/etc/smb.conf",
  160.   );
  161.  
  162.   my $dist = $dist_map {$Utils::Backend::tool{"platform"}};
  163.   return $dist_tables{$dist} if $dist;
  164.   return undef;
  165. }
  166.  
  167. sub get_share_info
  168. {
  169.   my ($smb_conf_name, $section) = @_;
  170.   my @share;
  171.  
  172.   push @share, $section;
  173.   push @share, &Utils::Parse::get_from_ini      ($smb_conf_name, $section, "path");
  174.   push @share, &Utils::Parse::get_from_ini      ($smb_conf_name, $section, "comment");
  175.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "available");
  176.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "browsable") ||
  177.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "browseable");
  178.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "public")      ||
  179.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "guest");
  180.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "writable")    ||
  181.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "writeable");
  182.  
  183.   return \@share;
  184. }
  185.  
  186. sub set_share_info
  187. {
  188.   my ($smb_conf_file, $share) = @_;
  189.   my ($section);
  190.  
  191.   $section = shift (@$share);
  192.  
  193.   &Utils::Replace::set_ini        ($smb_conf_file, $section, "path",      shift (@$share));
  194.   &Utils::Replace::set_ini        ($smb_conf_file, $section, "comment",   shift (@$share));
  195.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "available", shift (@$share));
  196.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "browsable", shift (@$share));
  197.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "public",    shift (@$share));
  198.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "writable",  shift (@$share));
  199.  
  200.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "browseable");
  201.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "guest");
  202.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "writeable");
  203. }
  204.  
  205. sub get_shares
  206. {
  207.   my ($smb_conf_file) = @_;
  208.   my (@sections, @table, $share);
  209.  
  210.   # Get the sections.
  211.   @sections = &Utils::Parse::get_ini_sections ($smb_conf_file);
  212.  
  213.   for $section (@sections)
  214.   {
  215.     next if ($section =~ /^(global)|(homes)|(printers)|(print\$)$/);
  216.     next if (&Utils::Parse::get_from_ini_bool ($smb_conf_file, $section, "printable"));
  217.  
  218.     $share = &get_share_info ($smb_conf_file, $section);
  219.     push @table, $share;
  220.   }
  221.  
  222.   return \@table;
  223. }
  224.  
  225. sub get_smb_users
  226. {
  227.   my ($fd, @info, $users);
  228.  
  229.   $fd = &Utils::File::run_pipe_read ("pdbedit -L");
  230.   return [] if (!$fd);
  231.  
  232.   while (<$fd>)
  233.   {
  234.     chomp;
  235.     @info = split (/:/, $_);
  236.     push @$users, [ $info[0], "" ];
  237.   }
  238.  
  239.   return $users;
  240. }
  241.  
  242. sub get
  243. {
  244.   my ($smb_conf_file);
  245.   my ($shares, $workgroup, $desc, $wins, $winsserver, $users);
  246.  
  247.   $smb_conf_file = &get_distro_smb_file;
  248.   $shares = &get_shares ($smb_conf_file);
  249.  
  250.   $workgroup = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "workgroup");
  251.   $smbdesc = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "server string");
  252.   $wins = &Utils::Parse::get_from_ini_bool ($smb_conf_file, "global", "wins support");
  253.   $winsserver = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "wins server");
  254.   $users = &get_smb_users ();
  255.  
  256.   return ($shares, $workgroup, $smbdesc, $wins, $winsserver, $users);
  257. }
  258.  
  259. sub set_shares
  260. {
  261.   my ($smb_conf_file, $shares) = @_;
  262.   my (@sections, $section, $share);
  263.  
  264.   # Get the sections.
  265.   @sections = &Utils::Parse::get_ini_sections ($smb_conf_file);
  266.  
  267.   # remove deleted sections
  268.   foreach $section (@sections)
  269.   {
  270.     next if ($section =~ /^(global)|(homes)|(printers)|(print\$)$/);
  271.     next if (&Utils::Parse::get_from_ini_bool ($smb_conf_file, $section, "printable"));
  272.  
  273.     if (!&smb_table_find ($section, $shares))
  274.     {
  275.       Utils::Replace::remove_ini_section ($smb_conf_file, $section);
  276.     }
  277.   }
  278.  
  279.   for $share (@$shares)
  280.   {
  281.     &set_share_info ($smb_conf_file, $share);
  282.   }
  283. }
  284.  
  285. sub set_smb_users
  286. {
  287.   my ($users) = @_;
  288.   my ($old_users, $user, $config, $state);
  289.   my ($hash, $old_hash, $pipe);
  290.  
  291.   $old_users = &get_smb_users ();
  292.  
  293.   foreach $user (@$users)
  294.   {
  295.     $$config{$$user[0]} |= 1;
  296.     $hash{$$user[0]} = $user;
  297.   }
  298.  
  299.   foreach $user (@$old_users)
  300.   {
  301.     $$config{$$user[0]} |= 2;
  302.     $old_hash{$$user[0]} = $user;
  303.   }
  304.  
  305.   foreach $i (keys %$config)
  306.   {
  307.     $state = $$config{$i};
  308.     $user = $hash{$i};
  309.  
  310.     if ($state == 1 || ($state == 3 && $$user[1]))
  311.     {
  312.       # User added, or password was modified
  313.       $user = $hash{$i};
  314.       $pipe = &Utils::File::run_pipe_write ("smbpasswd -s -a $$user[0]");
  315.       # Have to write the password twice
  316.       print $pipe "$$user[1]\n";
  317.       print $pipe "$$user[1]\n";
  318.       &Utils::File::close_file ($pipe);
  319.     }
  320.     elsif ($state == 2)
  321.     {
  322.       # User deleted
  323.       $user = $old_hash{$i};
  324.       &Utils::File::run ("pdbedit --delete -u $$user[0]");
  325.     }
  326.   }
  327. }
  328.  
  329. sub set
  330. {
  331.   my ($shares, $workgroup, $desc, $wins, $winsserver, $users) = @_;
  332.   my ($smb_conf_file);
  333.   my (@sections, $export);
  334.  
  335.   $smb_conf_file = &get_distro_smb_file;
  336.  
  337.   &set_shares ($smb_conf_file, $shares);
  338.  
  339.   &Utils::Replace::set_ini ($smb_conf_file, "global", "workgroup", $workgroup);
  340.   &Utils::Replace::set_ini ($smb_conf_file, "global", "server string", $desc);
  341.   &Utils::Replace::set_ini_bool ($smb_conf_file, "global", "wins support", $wins);
  342.   &Utils::Replace::set_ini ($smb_conf_file, "global", "wins server", ($wins) ? "" : $winsserver);
  343.  
  344.   &set_smb_users ($users);
  345. }
  346.  
  347. sub get_files
  348. {
  349.   my ($files);
  350.  
  351.   push @$files, &get_distro_smb_file ();
  352.   return $files;
  353. }
  354.  
  355. 1;
  356.